package me.hetong.caigou.util;

import cn.hutool.core.util.StrUtil;
import me.hetong.caigou.entity.Chess;
import me.hetong.caigou.entity.Zhao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ZhaoFaUtils {

    private static final Map<String, String> map = new HashMap<>();

    static {
        map.put("１", "1");
        map.put("２", "2");
        map.put("３", "3");
        map.put("４", "4");
        map.put("５", "5");
        map.put("６", "6");
        map.put("７", "7");
        map.put("８", "8");
        map.put("９", "9");
        map.put("0", "0");
        map.put("1", "１");
        map.put("2", "２");
        map.put("3", "３");
        map.put("4", "４");
        map.put("5", "５");
        map.put("6", "６");
        map.put("7", "７");
        map.put("8", "８");
        map.put("9", "９");
        map.put("8x", "一");
        map.put("7x", "二");
        map.put("6x", "三");
        map.put("5x", "四");
        map.put("4x", "五");
        map.put("3x", "六");
        map.put("2x", "七");
        map.put("1x", "八");
        map.put("0x", "九");
        map.put("1y", "一");
        map.put("2y", "二");
        map.put("3y", "三");
        map.put("4y", "四");
        map.put("5y", "五");
        map.put("6y", "六");
        map.put("7y", "七");
        map.put("8y", "八");
        map.put("9y", "九");
        map.put("space", "　");
    }

    /**
     * 判断是否红方在下，当红方在下是为true,红色在上为false
     *
     * @param fen
     * @return
     */
    private static boolean isRedDown(String fen) {
        if (StrUtil.isBlank(fen)) {
            return true;
        } else {
            String[] lines = fen.split("/");
            for (int i = 0; i < 3; i++) {
                String line = lines[i];
                if (line.contains("k")) {
                    return true;
                }
            }
        }
        return false;
    }


    public static String getZhaoFaName(String fen, String move) {
        if (StrUtil.isBlank(fen)) {
            fen = "rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR";
        }

        boolean redDown = isRedDown(fen);
        Zhao zhao = FenUtils.moveToZhao(move);

        if (!redDown) {
            fen = StrUtil.reverse(fen);
            FenUtils.reverseZhao(zhao);
        }

        List<Chess> chessList = FenUtils.fenToChessList(fen);
        getName(chessList, zhao);

        return zhao.getName();
    }

    public static String getZhaoFaName(List<Chess> chessList, Zhao zhao) {
        String fen = FenUtils.chessListToFen(chessList);
        String move = FenUtils.zhaoToMove(zhao);
        zhao.setName(getZhaoFaName(fen, move));
        return zhao.getName();
    }

    private static void getName(List<Chess> chessList, Zhao zhao) {
        final StringBuilder sb = new StringBuilder();

        Chess chess = null;
        for (final Chess li : chessList) {
            if (li.getX() == zhao.getX1() && li.getY() == zhao.getY1()) {
                chess = li;
                break;
            }
        }

        boolean red = chess.getType().equals("w");

        sb.append(chess.getName());
        if (red) {
            sb.append(map.get(zhao.getX1() + "x"));
        } else {
            sb.append(map.get(zhao.getX1() + 1 + ""));
        }

        if (Objects.equals(zhao.getY1(), zhao.getY2())) {
            sb.append("平");
            if (red) {
                sb.append(map.get(zhao.getX2() + "x"));
            } else {
                sb.append(map.get(zhao.getX2() + 1 + ""));
            }
        } else {
            if (red) {
                if (zhao.getY1() < zhao.getY2()) {
                    sb.append("退");
                } else {
                    sb.append("进");
                }
            } else {
                if (zhao.getY1() < zhao.getY2()) {
                    sb.append("进");
                } else {
                    sb.append("退");
                }
            }
            if (chess.getK().equalsIgnoreCase("n") || chess.getK().equalsIgnoreCase("b") || chess.getK().equalsIgnoreCase("a")) {
                // 如果是马象士
                if (red) {
                    sb.append(map.get(zhao.getX2() + "x"));
                } else {
                    sb.append(map.get(zhao.getX2() + 1 + ""));
                }
            } else {
                final int abs = Math.abs(zhao.getY1() - zhao.getY2());
                if (red) {
                    sb.append(map.get(abs + "y"));
                } else {
                    sb.append(map.get(abs + ""));
                }
            }
        }

        zhao.setName(sb.toString());
    }

    public static void main(String[] args) {
    }
}